﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using XYAC.Framework.MonitorTireMaterialOnline.Context;
using XYAC.Framework.MonitorTireMaterialOnline.Entities;
using XYAC.Framework.MonitorTireMaterialOnline.Extensions;

namespace XYAC.Framework.MonitorTireMaterialOnline.Services
{
    public class AccessCAreaService
    {
        public AccessCAreaService(DatabaseContext baseDb, HisDataContext hisDb, IServiceProvider provider)
        {
            this.Provider = provider;
            //this.BaseDb = baseDb;
            //this.HisDb = hisDb;
        }

        public IServiceProvider Provider { get; }

        //public DatabaseContext BaseDb { get; }
        //public HisDataContext HisDb { get; }

        internal int Add(SpecInfo specInfo)
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                BaseDb.Add(specInfo);
                return BaseDb.SaveChanges();
            }
        }
        internal int AddSpecInfo(SpecInfo findedRemoteSpec)
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();

                BaseDb.SpecInfo.Add(findedRemoteSpec);
                return BaseDb.SaveChanges();
            }
        }
        internal List<SpecInfo> GetAllSpecInfo()
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();
                var calc = new CrcCalculator();
                var specinfos = BaseDb.SpecInfo.ToList();
                return specinfos;
            }
        }
        internal SpecValTable GetLastSpecVal()
        {
            using (var scope = Provider.CreateScope())
            {
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();
                return HisDb.SpecVal.OrderByDescending(T => T.ID).FirstOrDefault();
            }
        }

        internal object GetSpecInfoById(int iD)
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();
                return BaseDb.SpecInfo.FirstOrDefault(T => T.Id == iD);
            }
        }
        internal IList<SpecInfo> GetSpecInfos()
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();
                //return this.BaseDb.SpecInfo.ToList();
                var x = BaseDb.SpecInfo.ToList();
                CrcCalculator calculator = new CrcCalculator();
                //x.ForEach(T => T.Hash = calculator.ComputeCrc128(JsonSerializeHelper.SerializeObjectToJson(T)).ToString());
                return x;
            }
        }


        internal int RemoveViaSpecNames(IEnumerable<string> preRemoveSpecKey)
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();
                var specinfos = BaseDb.SpecInfo.Where(T => preRemoveSpecKey.Contains(T.ProfileName));
                BaseDb.SpecInfo.RemoveRange(specinfos);
                return BaseDb.SaveChanges();
            }
        }

        internal bool SaveRemoteSpecInfoToLocal(SpecInfo findedRemoteSpec)
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();
                var entity = BaseDb.SpecInfo.FirstOrDefault(T => T.ProfileName == findedRemoteSpec.ProfileName);
                BaseDb.Entry(entity).CurrentValues.SetValues(findedRemoteSpec);
                BaseDb.Entry(entity).State = EntityState.Modified;
                var ret = BaseDb.SaveChanges() > 0;

                return ret;
            }
        }

        internal int Update(SpecInfo specInfo)
        {
            using (var scope = Provider.CreateScope())
            {
                var BaseDb = scope.ServiceProvider.GetService<DatabaseContext>();
                var HisDb = scope.ServiceProvider.GetService<HisDataContext>();
                var spec = BaseDb.SpecInfo.FirstOrDefault(T => T.Id == specInfo.Id);
                BaseDb.Entry(spec).CurrentValues.SetValues(specInfo);
                BaseDb.Entry(spec).State = EntityState.Modified;
                return BaseDb.SaveChanges();
            }
        }
    }
}
